﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.Collapse
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class EntityCollapseOutput:ResultBase
    {
        #region props
        private int _CollapsedEntitiesCount;
        /// <summary></summary>
        public int CollapsedEntitiesCount
        {
            get
            {
                return this._CollapsedEntitiesCount;
            }
            set
            {
                this._CollapsedEntitiesCount = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public EntityCollapseOutput():base(Guid.Empty,Guid.Empty)
        {
            this._CollapsedEntitiesCount = 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="collapsedCount"></param>
        public EntityCollapseOutput(Guid componentID, Guid jobTicketID,
            int collapsedCount) 
            : base(componentID, jobTicketID)
        {
            this._CollapsedEntitiesCount = collapsedCount;
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xDoc"></param>
        /// <param name="parentNode"></param>
        /// <returns></returns>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Output");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FinishTime", this.FinishTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "CollapsedEntitiesCount", this.CollapsedEntitiesCount.ToString());

            return xNode;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xNode"></param>
        /// <returns></returns>
        public override ResultBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                EntityCollapseOutput output = new EntityCollapseOutput();
                output.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                output.FinishTime = DateTime.Parse(xNode.Attributes.GetNamedItem("FinishTime").Value);

                output.CollapsedEntitiesCount =
                    int.Parse(XmlDataUtil.GetAttributeValue(xNode, "CollapsedEntitiesCount", "0"));

                return output;
            }
            else
            {
                throw new Exception("Unable to instantiate output: invalid type specified in xml node");
            }
        }


        #region serialization
        private string SerialzieFieldValue(int[] fieldValues)
        {
            string value = null;
            BinaryFormatter bSerializer = new BinaryFormatter();
            string tmpFilePath = FileUtil.GetWindowsAppTempFilePath("ORMapping", "orm");
            FileStream fs = File.OpenWrite(tmpFilePath);
            bSerializer.Serialize(fs, fieldValues);
            fs.Close();

            fs = File.OpenRead(tmpFilePath);
            byte[] contents = new byte[fs.Length];
            fs.Read(contents, 0, contents.Length);
            fs.Close();
            value = Base64Util.EncodeAsBase64(contents);

            File.Delete(tmpFilePath);

            return value;
        }

        private int[] InstantiateFieldValue(string serializedString)
        {
            int[] fieldValues = null;
            BinaryFormatter bSerializer2 = new BinaryFormatter();
            string tmpFilePath = FileUtil.GetWindowsAppTempFilePath("ORMapping", "orm");
            FileStream fs2 = File.OpenWrite(tmpFilePath);
            byte[] contents2 = Base64Util.DecodeAsBytes(serializedString);
            fs2.Write(contents2, 0, contents2.Length);
            fs2.Flush();
            fs2.Close();
            fs2 = File.OpenRead(tmpFilePath);
            fieldValues = (int[])bSerializer2.Deserialize(fs2);
            fs2.Close();
            File.Delete(tmpFilePath);

            return fieldValues;
        }
        #endregion
    }
}
